package utn.frd.form;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utn.frd.domain.Persona;

public class FormPersona extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	private HashMap<String,Persona> persons = new HashMap<String,Persona>(); 
	
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    	try{
	    	Persona p = new Persona();
			p.setNombre( request.getParameter("name") );
			p.setCiudad( request.getParameter("city") );
			p.setDNI( request.getParameter("dni") );
			if( request.getParameter("age")!=null )
				p.setEdad( Integer.parseInt(request.getParameter("age")) );
				
			persons.put( request.getParameter("dni"), p );	
			
    	}catch (Exception e) {
			request.setAttribute("error", e.getMessage());
		}
		
		request.setAttribute("persons", new ArrayList<Persona>( persons.values() ));
    	
    	request.getRequestDispatcher("formulario.jsp").forward(request, response);
	}
}
